﻿$(function () {
    $.getJSON("/Post/LoadDDLCategory", { id: "1" }, function (catData) {
        var select = $("#ddlCategory");
        select.empty();
        select.append($('<option/>', {
            value: 0,
            text: "Select a Category"
        }));
        $.each(catData, function (index, itemData) {

            select.append($('<option/>', {
                value: itemData.Value,
                text: itemData.Text
            }));
        });

        if ($("#Title").val() != "") {
            $("#addpostSectionAddress").fadeIn();
        }
    });

    $("#btncreatepost").click(function () {
        var errorMsg = "";
        var categoryId = $("#ddlCategory").val();
        var title = $.trim($("#Title").val());
        var description = $("#Description").val();
        var country = $("#ddlCountry").val();
        var state = $("#ddlState").val();
        var city = $("#ddlCity").val();
        var email = $("#PostCreatorEmail").val();
        var name = $("#PostCreatorName").val();
        var contactPhone = $("#ContactPhone").val();

        if (categoryId == null || categoryId == "0") {
            errorMsg = errorMsg + " category ";
        }
        if (title == "") {
            errorMsg = errorMsg + " title ";
        }
        if ($.trim(description) == "") {
            errorMsg = errorMsg + " description ";
        }
        if (country == null  || country == "" ||country == "0") {
            errorMsg = errorMsg + " country ";
        }
        if (state == null || state == "" || state == "0") {
            errorMsg = errorMsg + " state/region ";
        }
        if (city == null || city == "0" || city == "") {
            errorMsg = errorMsg + " city ";
        }
        if ($.trim(name) == "") {
            errorMsg = errorMsg + " name ";
        } 
        if ($.trim(email) == "") {
            errorMsg = errorMsg + " email";
        } 
        else 
        {
        //validate email
            var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test(email)) {
                errorMsg = errorMsg + " valid email";
            }
        }

        if (errorMsg != "") {
            errorMsg = errorMsg.replace(/  /g, ", ");
            errorMsg = "Please fill in" + errorMsg + ".";
            alert(errorMsg);
        }
/* Phone validation will go here.
        else if (contactPhone != "" || contactPhone.match('/^\(\d{3}\)\s?\d{3}-\d{4}$/')) {
            errorMsg = "Phone format is not valid";
        } 
 */       
        else {
            
            var createPostModel = {
                Needed: $('#Needed[type="radio"]:checked').val(),
                CategoryID: categoryId,
                Title: title,
                Description: description,
                ContactPhone: $("#ContactPhone").val(),
                Country: country,
                State: state,
                City: city,
                Address1: $("#Address1").val(),
                Address2: $("#Address2").val(),
                ZipCode: $("#ZipCode").val(),
                ShowEmailInPost: $("#ShowEmailInPost").prop('checked'),
                PostCreatorEmail: email
            };

            $.ajax({
                url: '/Post/CreatePost',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(createPostModel),
                contentType: 'application/json; charset=utf-8',
                success: function(data) {
                    if (data.success) {
                        window.location.href = data.redirectToUrl;
                    } else {
                        if (data.result == "validationError") {
                            alert("There is a problem with the form. ");
                        }
                    }
                },
                error: function () {
                    alert("An error occured while creating your post. Please try again later.");
                }
            });
        }
    });
});